//
//  SettingVC.m
//  DriverClient
//
//  Created by 吕晶 on 2020/8/3.
//  Copyright © 2020 吕晶. All rights reserved.
//

#import "SettingVC.h"
#import "SettingCell.h"
#import "SettingLogOutCell.h"
#import "PersonInfoVC.h"
#import "VerifyPwdAlertView.h"
#import "SetNewPwdVC.h"
#import "ModifyPhoneVC.h"
#import "ModifyEmailVC.h"
#import "PersonInfoModel.h"

@interface SettingVC ()<UITableViewDelegate,UITableViewDataSource> {
    BOOL isSetPwd;
    NSString *phone;
}

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) PersonInfoModel *infoModel;

@end

@implementation SettingVC

-(PersonInfoModel *)infoModel {
    if (!_infoModel) {
        _infoModel = [PersonInfoModel new];
    }
    return _infoModel;
}

-(UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH - NaviHeight - BottomHeight) style:UITableViewStyleGrouped];
        _tableView.backgroundColor = Color_backgroundColor;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"设置";
    isSetPwd = NO;
    phone = @"";
    [self.view addSubview:self.tableView];
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self requestForData];
}

- (void)requestForData {
    [HttpRequest PostWithApi:LogisticsSupplierGetSet params:@{} progress:^(NSProgress * _Nonnull progress) {

    } success:^(NSURLSessionDataTask * _Nonnull operation, id  _Nonnull responseObject, BOOL code, NSInteger errorCode) {
        if (code) {
            self.infoModel = [PersonInfoModel mj_objectWithKeyValues:responseObject];
            [self.tableView reloadData];
        }
    } fail:^(NSError * _Nonnull error) {

    }];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 3;
    }
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        SettingCell *cell = [SettingCell SettingCellWithTableView:tableView];
//        else if (indexPath.row == 1) {
//            cell.titleText = @"密码修改";
//            cell.descText = @"";
//            cell.showLine = YES;
//        }
        if (indexPath.row == 0) {
            cell.titleText = @"个人资料";
            cell.descText = @"";
            cell.showLine = YES;
        }else if (indexPath.row == 1) {
            cell.titleText = @"邮箱绑定";
            cell.descText = self.infoModel.email.length ? self.infoModel.email : @"未设置";
            cell.showLine = YES;
        }else {
            cell.titleText = @"手机绑定";
            cell.descText = self.infoModel.phone;
            cell.showLine = NO;
        }
        return cell;
    }else {
        SettingLogOutCell *cell = [SettingLogOutCell SettingLogOutCellWithTableView:tableView];
        return cell;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        SettingCell *cell = [SettingCell SettingCellWithTableView:tableView];
        return cell.height;
    }else {
        SettingLogOutCell *cell = [SettingLogOutCell SettingLogOutCellWithTableView:tableView];
        return cell.height;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            PersonInfoVC *vc = [PersonInfoVC new];
            [self.rt_navigationController pushViewController:vc animated:YES];
        }else if (indexPath.row == 1) {
//            VerifyPwdAlertView *alert = [VerifyPwdAlertView new];
//            __weak_self;
//            alert.sureBlock = ^{
//                __strong_self;
                ModifyEmailVC *vc = [ModifyEmailVC new];
                vc.email = self.infoModel.email;
                [self.rt_navigationController pushViewController:vc animated:YES];
//            };
//            [alert showInView:MAIN_WINDOW];
        }else if (indexPath.row == 2) {
//            VerifyPwdAlertView *alert = [VerifyPwdAlertView new];
//            __weak_self;
//            alert.sureBlock = ^{
//                __strong_self;
                ModifyPhoneVC *vc = [ModifyPhoneVC new];
                vc.phone = self.infoModel.phone;
                [self.rt_navigationController pushViewController:vc animated:YES];
//            };
//            [alert showInView:MAIN_WINDOW];
        }
    }else {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"确定退出登录？" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [User logoutSuccess];
        }];
        UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        [alert addAction:sureAction];
        [alert addAction:cancleAction];
        [self presentViewController:alert animated:YES completion:nil];
    }
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, 10)];
    view.backgroundColor = [UIColor clearColor];
    return view;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [UIView new];
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0;
}

@end
